//
//  MainViewController.swift
//  weibo
//
//  Created by 谭仁川 on 15/11/27.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    let mainTabBar = MainTabBar()
   
    @objc private func composeBtnDidClick(){
    
        print(__FUNCTION__)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
      
        setValue(mainTabBar, forKey: "tabBar")
        
        mainTabBar.composeBtn.addTarget(self, action: "composeBtnDidClick", forControlEvents: .TouchUpInside)
        addChildViewControllers()
    }
    func composeDidClick(){
        print(__FUNCTION__)
    }
    private func setupUI(){
    
        //tabBar.delegate = self
        
      // tabbar.composeBtn.addTarget(self, action: "composeDidClick", forControlEvents: .TouchUpInside)
        
        setValue(tabBar, forKey: "tabBar")
        
        addChildViewControllers()
    }
    
    func addChildViewControllers() {
        
        addChildViewControllers(HomeTableViewController(), name: "首页", imageName: "tabbar_home", index: 0)
        addChildViewControllers(MessageTableViewController(), name: "消息", imageName: "tabbar_message_center", index: 1)
        
        addChildViewControllers(DIscoverTableViewController(), name: "发现", imageName: "tabbar_profile", index: 3)
        addChildViewControllers(ProfileTableViewController(), name: "我", imageName: "tabbar_profile", index: 3)
        
        
    }
    
    private func addChildViewControllers(vc: UIViewController,name: String ,imageName: String,index: Int) {
        
        
        let nav = BaseNavController(rootViewController:vc)
        
        vc.title = name
        
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.tag = index
        addChildViewController(nav)
        
    }
    
    
}
extension MainViewController {
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        
        print(item.tag)
        //定义标记
        var index = 0
        //遍历item的所有子视图
        for itemSubView in tabBar.subviews {
            //取出所有的按钮
            if itemSubView .isKindOfClass(NSClassFromString("UITabBarButton")!) {
                if index == item.tag {
                    //遍历所有的子视图 取到imageView
                    for v in itemSubView.subviews {
                        if v.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!) {
                            //开始动画
                            v.transform = CGAffineTransformMakeScale(0.8, 0.8)
                            UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.1, initialSpringVelocity: 0, options: [], animations: { () -> Void in
                                v.transform = CGAffineTransformMakeScale(1, 1)
                                }, completion: { (_) -> Void in
                                   // print("👌")
                            })
                        }
                    }
                    
                }
                index++
            }
        }
    }
}
